using System.IO.Ports;

namespace Sensor.SerialPorts;

/// <summary>
/// 串口辅助类，提供串口相关的静态辅助方法
/// 包括获取可用串口名称、校验位和停止位的转换功能
/// </summary>
public static class SerialPortHelp
{
    #region 公共方法
    /// <summary>
    /// 获取当前系统所有可用的串口名称列表
    /// </summary>
    /// <returns>包含所有可用串口名称的列表</returns>
    public static List<string> GetPortNames()
    {
        return [.. SerialPort.GetPortNames()];
    }

    /// <summary>
    /// 将字符串表示的校验位转换为 Parity 枚举值
    /// </summary>
    /// <param name="parity">校验位的字符串表示，支持 "None"、"Odd"、"Even"</param>
    /// <returns>对应的 Parity 枚举值，无效输入时返回 Parity.None</returns>
    public static Parity GetParity(string parity)
    {
        var result = parity switch
        {
            "None" => Parity.None,
            "Odd" => Parity.Odd,
            "Even" => Parity.Even,
            _ => Parity.None,
        };
        return result;
    }

    /// <summary>
    /// 将字符串表示的停止位转换为 StopBits 枚举值
    /// </summary>
    /// <param name="stopBits">停止位的字符串表示，支持 "One"、"Two"、"OnePointFive"</param>
    /// <returns>对应的 StopBits 枚举值，无效输入时返回 StopBits.None</returns>
    public static StopBits GetStopBits(string stopBits)
    {
        var result = stopBits switch
        {
            "One" => StopBits.One,
            "Two" => StopBits.Two,
            "OnePointFive" => StopBits.OnePointFive,
            _ => StopBits.None,
        };
        return result;
    }
    #endregion
}
